+function(){
	
	window['bgOpts'] = {page:"Contract"}
	//是否启用自动填充
	bgOpts.method="GetShowContractImport";
	bg.getInfo(function(resp){
		if(resp.obj){
			bgOpts.method = "GetContractInfo";
			//获取需要设置的合同信息
			bg.getInfo(function(resp){
				if(resp.code == 400 && resp.obj != undefined){
					c.log("后台获取到的ContractInfo:"+resp.obj.TextOne);
					/*************设置信息到页面***************/
					//根据版本获取obj对象并填充到表单
					GetObjAndFill(resp.obj,function(tip){
						//点击确认收款弹出对话框
						$("input[name=btnTiJiao][value=确认收款]").click();
						//修改提示内容
						$(".l-dialog .l-dialog-content").html(tip);
						//删除按钮[是]
						$(".l-dialog .l-dialog-buttons .l-dialog-btn-ok").remove();
						//获取延时时间
						bgOpts.method = "GetContractWaitConfirmTime";
						bg.getInfo(function(r){
							var d = 2;
							if(r.code == 400){
								c.log("延时:"+r.obj+" 秒");
								d = parseInt(r.obj);
							}
							//设置对话框按钮[否]的文本内容
							changeCancelText(0,resp.obj,d);
							var index = c.delay(d,clickOk,changeCancelText,resp.obj);
						});
					});
				}
			});
		}
	});
}();
//根据版本获取数据类并填充数据到表单
function GetObjAndFill(obj,callback){
	//获取页面显示的单号
	c.log(obj);
	if(obj.version == 2){
		//是春版，则需要从后台获取obj的详细信息
		
		var url = window.location.origin+"/handler/Pay_Back/PayBackHander.ashx?oper=QueryPayBackByTradeNo&tradeNo="+obj.TransactionNo;
		$.get(url,function(res){
			var r = $.parseJSON(res);
			//判断是否获取成功
			if(r.success){
				var message = $.parseJSON(r.message);
				if(message.length>0){
					//获取第一个结果
					var mess = message[0];
					obj.PayTime=mess.FTRANSDATE;
					obj.PayAmount=mess.FCREDITAMOUNT;
					obj.ReceivedCompany=mess.FCreateOrgName;
					obj.ReceivedAccoun=mess.FBANKID;
					obj.Description=mess.Usage;
					obj.ReceivedType=mess.FBookedType;
					//填充表单数据
					var tip = FillFormData(obj);
					//执行回调
					callback(tip);
				}else{
					contractSubmitError(obj,"不存在此交易流水号的回款信息");
					closeCurrentWindow();
				}
			}else{
				contractSubmitError(obj,r.message);
				closeCurrentWindow();
			}
		});
	}else{
		//秋版，则直接填充数据
		var tip = FillFormData(obj);
		callback(tip);
	}
}
//填充数据
function FillFormData(obj){
	//设置打款时间
	$("#txtPayTime").val(obj.PayTime);
	//打款金额
	$("#txtPayAmount").val(obj.PayAmount);
	//收款公司
	$("#txtReceivedCompany").val(obj.ReceivedCompany);
	//收款账户
	$("#txtReceivedAccoun").val(obj.ReceivedAccoun);
	//备注
	$("#txtDescription").val(obj.Description);
	//凭证收取方式
	$("#txtReceivedType").val(obj.ReceivedType);
	//交易单号
	$("#txtTransactionNo").val(obj.TransactionNo);
	var tip = "";
	tip += "开户单号:"+obj.TextOne+"<br/>";
	tip += "打款时间:"+obj.PayTime+"<br/>";
	tip += "金额:"+obj.PayAmount+"<br/>";
	tip += "收款公司:"+obj.ReceivedCompany+"<br/>";
	tip += "收款账户:"+obj.ReceivedAccoun+"<br/>";
	tip += "凭证收取方式:"+obj.ReceivedType+"<br/>";
	tip += "交易单号:"+obj.TransactionNo+"<br/>";
	tip += "备注:"+obj.Description+"<br/>";
	return tip;
}
function getSimpleName(str){
	var tmp = {
		"POS机":"POS",
		"支付宝":"ZFB",
		"微信":"WX",
		"银联":"YL"
	}
	return tmp[str];
}
//弹出确认对话框3秒后执行
function clickOk(obj){
	//查找对话框窗口
	$el = $(".l-dialog");
	if($el.length<=0){
		//用户点击了否取消了确认
		contractSubmitError(obj,"用户取消了确认收款");
		closeCurrentWindow();
	}else{
		/***********用户没有点击取消,进行提交****************/
		submitForm(obj);
	}
	
	
}
//弹出确认对话框后每秒做的事情
function changeCancelText(i,obj,time){
	//查找对话框窗口
	c.log("等待确认:"+(time-i));
	$(".l-dialog .l-dialog-buttons .l-dialog-btn-no .l-dialog-btn-inner").html("否["+(time-i)+"]");
	//判断对话框是否还存在
	$el = $(".l-dialog");
	return $el.length<=0;
}
//合同提交错误，向后台发送错误消息
function contractSubmitError(obj,Msg,success){
	success=success||false;
	var m = "["+obj.TextOne+"]:"+Msg;
	c.warn(m);
	bgOpts.method="AddContractError";
	obj.success=success;
	obj.errorMsg = m;
	//错误提交到后台
	bgOpts.obj=obj;
	bg.getInfo(function(resp){});
}
//提交表单
function submitForm(obj){
	obj.ID = c.getQueryString("Id");
	c.showLoading("确认中......");
    $.ajax({
        url: '/handler/Home_Contract/CurrentContract.ashx?oper=SureReceipt',
        dataType: "text",
        data: obj,
        success: function (e) {
        	c.closeLoading();
            var data = $.parseJSON(e); //将返回的字符串转JSON
            c.log(data);
            if (data.success) {
            	//成功
				contractSubmitError(obj,data.message,true);
				closeCurrentWindow();
            } else {
                //失败
				contractSubmitError(obj,data.message);
				closeCurrentWindow();
            }
        }
    })
}
//关闭当前窗口
function closeCurrentWindow(){
	//清楚掉后台的obj
	bgOpts.method = "clearContractInfo";
	bg.getInfo(function(resp){
		//调用父窗口关闭此对话框
		parent.CloseContractSureReceipt();
	});
}
